﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

/// <summary>
/// 带有过滤器队列的Mono
/// </summary>
public abstract class ReceiverBase : MonoBehaviour
{
    /// <summary>
    /// 开启日志
    /// </summary>
    [Header("开启日志")]
    public bool openLog = true;
    /// <summary>
    /// 是否生效
    /// </summary>
    [Header("是否生效")]
    public bool enable = true;
    /// <summary>
    /// 过滤器队列
    /// </summary>
    [Header("过滤器队列")]
    public List<FilterBase> filterQueue = new();

    private void Awake()
    {
        if (GetComponent<ICanvasRaycastFilter>() == null)
        {
            Debug.LogError("Receiver need ICanvasRaycastFilter");
        }
    }

    /// <summary>
    /// 通过过滤器
    /// </summary>
    /// <param name="results"></param>
    /// <returns></returns>
    protected List<RaycastResult> PassFilterQueue(List<RaycastResult> results)
    {
        if (!enable)
        {
            return null;
        }
        if (filterQueue == null
            || filterQueue.Count == 0)
        {
            Debug.Log("FilterQueue is null");
            return results;
        }
        LogRaycastResult("BeforePass", results);
        for (int i = 0; i < filterQueue.Count; i++)
        {
            var filter = filterQueue[i];
            results = filter.FiltResults(results);
            LogRaycastResult($"AfterFilter {filter.GetType()}", results);
        }
        return results;
    }

    /// <summary>
    /// 过滤结果日志
    /// </summary>
    /// <param name="title"></param>
    /// <param name="results"></param>
    private void LogRaycastResult(string title, List<RaycastResult> results)
    {
        if (!openLog) return;
        string resultStr = "";
        for (int i = 0; i < results.Count; i++)
        {
            resultStr += results[i].gameObject.name;
            if (i < results.Count - 1)
                resultStr += " ==> ";
        }
        Debug.Log(title + " : " + resultStr);
    }
}
